package main

import (
	"fmt"
	"zinx/ziface"
	"zinx/znet"
)

// ping test 自定义路由
type HelloRouter struct {
	znet.BaseRouter
}

// 模版方法设计模式

// Test Handle
func (r *HelloRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call  Hello Router Handle ....")
	// 先读取客户端的数据，再会写ping....ping...
	fmt.Println("recv from client: msgID = ", request.GetMsgId(), ", data = ", string(request.GetData()))
	err := request.GetConnection().SendMsg(200, []byte("hello....hello...hello..."))
	if err != nil {
		fmt.Println(err)
	}
}

// ping test 自定义路由
type PingRouter struct {
	znet.BaseRouter
}

// 模版方法设计模式

// Test Handle
func (r *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call Ping Router Handle ....")
	// 先读取客户端的数据，再会写ping....ping...
	fmt.Println("recv from client: msgID = ", request.GetMsgId(), ", data = ", string(request.GetData()))
	err := request.GetConnection().SendMsg(201, []byte("ping....ping...ping..."))
	if err != nil {
		fmt.Println(err)
	}
}

func main() {
	s := znet.NewServer("[zinx V0.6]")

	// 添加路由
	s.AddRouter(0, &PingRouter{})
	s.AddRouter(1, &HelloRouter{})
	s.Serve()
}
